
package com.hl.card.modular.pack.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hl.card.common.persistence.dao.CommunityUserMapper;
import com.hl.card.common.persistence.dao.ZoneUserAwardMapper;
import com.hl.card.common.persistence.dao.ZoneUserAwardThreeMapper;
import com.hl.card.common.persistence.model.CommunityUser;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.persistence.model.ZoneUserAward;
import com.hl.card.common.persistence.model.ZoneUserAwardThree;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.modular.pack.service.PackUserService;
import com.hl.card.modular.pack.service.RewardByCount;
import com.hl.card.modular.pack.service.UserDataService;
import com.hl.card.modular.system.service.ISysDicService;
import com.hl.card.mq.Mq4NotifyDemo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class PackUserServiceImpl implements PackUserService {

    @Autowired
    private UserDataService userDataService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RewardByCount rewardByCount;
    @Autowired
    private ZoneUserAwardMapper zoneUserAwardMapper;

    @Autowired
    private ZoneUserAwardThreeMapper zoneUserAwardThreeMapper;
    @Autowired
    CommunityUserMapper communityUserMapper;
    @Autowired
    ISysDicService sysDicService;
    @Autowired
    Mq4NotifyDemo mq4NotifyDemo;


    /**
     * @return
     */
    @Override
    public Boolean zoneUserAwardRetry() {
        Wrapper<ZoneUserAward> wrapper = new EntityWrapper<ZoneUserAward>();
        wrapper.eq("award_result", 0);
        List<ZoneUserAward> awardList = zoneUserAwardMapper.selectList(wrapper);
        ZoneUserAward award;
        for (int i = 0; i < awardList.size(); i++) {
            award = awardList.get(i);
            if (award.getUserId() != null) {
                //资料完整，进行补发
//                if(award.getAwardResult() == 1 && award.getUserIdAward() > 0) {
//                    award.setAmount(award.getAwardArrive().intValue());
//                }else
                {
                    try {
                        ResponseObject rob = rewardByCount.doWxCommunityReward(award.getCommunityAdmin(), award.getRecommender(), award.getUserId(), award.getAmount().toString());
                        Object strObj = rob.getData();
                        if (rob.getErroCode().equals(1)) {//领取失败
                            award.setAwardResult(0);
                            System.out.println("领取社区推荐奖励失败：" + award.getUserId());
                        } else if (rob.getErroCode().equals(0)) {//领取成功
                            award.setAwardResult(1);
                            JSONObject jsonObject = JSON.parseObject(String.valueOf(strObj));
                            System.out.println("领取社区推荐奖励成功：" + award.getUserId() + "：" + jsonObject.toJSONString());
                            Integer communityAdmin_award = Integer.parseInt(jsonObject.get("communityAdmin").toString());
                            Integer recommender_award = Integer.parseInt(jsonObject.get("recommender").toString());
                            Integer userId_award = Integer.parseInt(jsonObject.get("userId").toString());
                            award.setCommunityAdminAward(communityAdmin_award);
                            award.setRecommenderAward(recommender_award);
                            award.setUserIdAward(userId_award);
                            award.setAmount(award.getAwardArrive().intValue());
                            //奖励之后,回填
                            //上级是群主
                            if (award.getRecommender().equals(award.getCommunityAdmin())) {
                                CommunityUser communityUserInfo = new CommunityUser();
                                communityUserInfo.setUser_id(award.getRecommender());
                                communityUserInfo.setHtl_count(recommender_award + communityAdmin_award);//推荐奖励+群主奖励
                                communityUserInfo.setUser_count(1);
                                communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                            } else {
                                //上级的上级是群主
                                CommunityUser communityUserInfo = new CommunityUser();
                                communityUserInfo.setUser_id(award.getCommunityAdmin());
                                communityUserInfo.setHtl_count(communityAdmin_award);
                                communityUserInfo.setTwo_user_count(1);
                                communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                            }
                        }
                    } catch (Exception e) {
                        award.setAmount(0);
                        award.setAwardResult(0);
                        e.printStackTrace();
                    }
                }
                zoneUserAwardMapper.updateById(award);
            }
        }
        return true;
    }

    /**
     * @return
     */
    @Override
    public Boolean zoneUserAwardRetryByRecommender() {
        Wrapper<ZoneUserAward> wrapper = new EntityWrapper<ZoneUserAward>();
        wrapper.eq("amount", 0);
        List<ZoneUserAward> awardList = zoneUserAwardMapper.selectList(wrapper);
        ZoneUserAward award, tempRecommenderAward;
        User recommender = new User();
        Wrapper<User> wrapper1 = new EntityWrapper<>();
        List<User> users;
        User user;
        List<ZoneUserAward> recommenderAwardList;
        List<User> recommenderUserList;
        Map<String, List<ZoneUserAward>> recommenderAwardMap = new HashMap<>();
        Map<String, List<User>> recommenderUsersMap = new HashMap<>();
        int count = 0;
        for (int i = 0; i < awardList.size(); i++) {
            award = awardList.get(i);
            recommenderAwardList = recommenderAwardMap.get(award.getRecommender());
            if (recommenderAwardList == null) {
                recommenderAwardList = new ArrayList<ZoneUserAward>();
                //查找所有推荐的用户
                wrapper1 = new EntityWrapper<>();
                wrapper1.eq("user_id", award.getRecommender());
                recommender = userDataService.selectOne(wrapper1);
                wrapper1 = new EntityWrapper<>();
                wrapper1.eq("refer_reg_code", recommender.getReg_code());
                users = userDataService.selectList(wrapper1);
                recommenderUsersMap.put(award.getRecommender(), users);
            }
            if (StringUtils.isBlank(award.getUserId())) {
                count++;
                award.setAmount(award.getAwardArrive().intValue());
                recommenderAwardList.add(award);
                recommenderAwardMap.put(award.getRecommender(), recommenderAwardList);
                System.out.println("手机用户为空数量：" + count);
            }
        }
        Iterator entries = recommenderAwardMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            String key = (String) entry.getKey();
            List<ZoneUserAward> values = (List<ZoneUserAward>) entry.getValue();

            for (int j = 0; j < values.size(); j++) {
                ZoneUserAward tempAward = values.get(j);
                users = recommenderUsersMap.get(tempAward.getRecommender());
                for (int i = 0; i < users.size(); i++) {
                    user = users.get(i);
                    tempRecommenderAward = new ZoneUserAward();
                    tempRecommenderAward.setUserId(user.getUserId());
                    tempRecommenderAward = zoneUserAwardMapper.selectOne(tempRecommenderAward);
                    if (tempRecommenderAward == null && StringUtils.isBlank(tempAward.getUserId())) {//该邀请用户未奖励
                        tempAward.setUserId(user.getUserId());
                        ResponseObject rob = rewardByCount.doWxCommunityReward(tempAward.getCommunityAdmin(), tempAward.getRecommender(), tempAward.getUserId(), tempAward.getAwardArrive().intValue() + "");
                        Object strObj = rob.getData();
                        if (rob.getErroCode().equals(1)) {//领取失败
                            tempAward.setAwardResult(0);
                            System.out.println("领取社区推荐奖励失败：" + tempAward.getUserId());
                        } else if (rob.getErroCode().equals(0)) {//领取成功
                            tempAward.setAwardResult(1);
                            JSONObject jsonObject = JSON.parseObject(String.valueOf(strObj));
                            System.out.println("领取社区推荐奖励成功：" + tempAward.getUserId() + "：" + jsonObject.toJSONString());
                            Integer communityAdmin_award = Integer.parseInt(jsonObject.get("communityAdmin").toString());
                            Integer recommender_award = Integer.parseInt(jsonObject.get("recommender").toString());
                            Integer userId_award = Integer.parseInt(jsonObject.get("userId").toString());
                            tempAward.setCommunityAdminAward(communityAdmin_award);
                            tempAward.setRecommenderAward(recommender_award);
                            tempAward.setUserIdAward(userId_award);
                            tempAward.setAmount(tempAward.getAwardArrive().intValue());
                            //奖励之后,回填
                            //上级是群主
                            if (tempAward.getRecommender().equals(tempAward.getCommunityAdmin())) {
                                CommunityUser communityUserInfo = new CommunityUser();
                                communityUserInfo.setUser_id(tempAward.getRecommender());
                                communityUserInfo.setHtl_count(recommender_award + communityAdmin_award);//推荐奖励+群主奖励
                                communityUserInfo.setUser_count(1);
                                communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                            } else {
                                //上级的上级是群主
                                CommunityUser communityUserInfo = new CommunityUser();
                                communityUserInfo.setUser_id(tempAward.getCommunityAdmin());
                                communityUserInfo.setHtl_count(communityAdmin_award);
                                communityUserInfo.setTwo_user_count(1);
                                communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                            }
                        }
                        zoneUserAwardMapper.updateById(tempAward);
                        break;
                    } else {
                        System.out.println("该用户已经领取过奖励：" + user.getUserId());
                    }
                }
            }
        }
        return true;
    }

    /**
     * @return
     */
    @Override
    public Boolean checkCommunityAdminInfo() {
        Wrapper<ZoneUserAward> wrapper;

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("offset", 1);
        map.put("size", 5000);
        List<CommunityUser> communityUserlist = communityUserMapper.list(map);
        CommunityUser communityUser;
        int communityAdminCount = 0;
        int recommenderCount = 0;
        for (int i = 0; i < communityUserlist.size(); i++) {
            communityUser = communityUserlist.get(i);
            wrapper = new EntityWrapper<ZoneUserAward>();
            wrapper.eq("communityAdmin", communityUser.getUser_id());
            List<ZoneUserAward> awardList = zoneUserAwardMapper.selectList(wrapper);
            communityAdminCount = 0;
            recommenderCount = 0;
            for (ZoneUserAward z : awardList) {
                if (z.getCommunityAdmin().equals(z.getRecommender())) {
                    communityAdminCount++;
                } else {
                    recommenderCount++;
                }
            }
            CommunityUser communityUserInfo = new CommunityUser();
            communityUserInfo.setUser_id(communityUser.getUser_id());
            communityUserInfo.setUser_count(communityAdminCount);
            communityUserInfo.setTwo_user_count(recommenderCount);
            communityUserMapper.updateCommunityUser(communityUserInfo);
        }
        return true;
    }

    @Override
    public Boolean checkCommunityAwardForFreeze() {
        Wrapper<ZoneUserAward> wrapper = new EntityWrapper<ZoneUserAward>();
        wrapper.eq("award_result", 1);//抽取所有奖励成功的订单
        List<ZoneUserAward> awardList = zoneUserAwardMapper.selectList(wrapper);
        ZoneUserAward award, tempRecommenderAward;
        User recommender = new User();
        Wrapper<User> wrapper1 = new EntityWrapper<>();
        List<User> users;
        User user;
        List<ZoneUserAward> recommenderAwardList;
        List<User> recommenderUserList;
        Map<String, List<ZoneUserAward>> recommenderAwardMap = new HashMap<>();
        Map<String, List<User>> recommenderUsersMap = new HashMap<>();
        int count = 0;
        for (int i = 0; i < awardList.size(); i++) {
            award = awardList.get(i);
            recommenderAwardList = recommenderAwardMap.get(award.getRecommender());
            if (recommenderAwardList == null) {
                recommenderAwardList = new ArrayList<ZoneUserAward>();
                //查找所有推荐的用户
                wrapper1 = new EntityWrapper<>();
                wrapper1.eq("user_id", award.getRecommender());
                recommender = userDataService.selectOne(wrapper1);
                if (recommender == null) {
                    continue;
                }
                wrapper1 = new EntityWrapper<>();
                wrapper1.eq("refer_reg_code", recommender.getReg_code());
                users = userDataService.selectList(wrapper1);
                recommenderUsersMap.put(award.getRecommender(), users);
            }
            count++;
            award.setAmount(award.getAwardArrive().intValue());
            recommenderAwardList.add(award);
            recommenderAwardMap.put(award.getRecommender(), recommenderAwardList);
            System.out.println("邀请用户数量：" + count);
        }
        Iterator entries = recommenderAwardMap.entrySet().iterator();
        Map<String, String> map;
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            String key = (String) entry.getKey();
            List<ZoneUserAward> values = (List<ZoneUserAward>) entry.getValue();

            for (int j = 0; j < values.size(); j++) {
                ZoneUserAward tempAward = values.get(j);
                users = recommenderUsersMap.get(tempAward.getRecommender());
                for (int i = 0; i < users.size(); i++) {
                    //未实名认证，奖励HTL冻结
                    if (users.get(i).getIsauth() == null || users.get(i).getIsauth() != 1) {
                        map = new HashMap<String, String>();
                        map.put("action", "freeze");
                        map.put("productName", "冻结未实名社区注册管理员奖励");
                        map.put("userId", tempAward.getCommunityAdmin());
                        map.put("mchOrderNo", "WXSQZC" + tempAward.getUserId());
                        map.put("amount", tempAward.getCommunityAdminAward().toString());
                        map.put("isFree", "isFree");
                        mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());

                        map = new HashMap<String, String>();
                        map.put("action", "freeze");
                        map.put("productName", "冻结未实名社区注册推荐人奖励");
                        map.put("userId", tempAward.getRecommender());
                        map.put("mchOrderNo", "WXSQZC" + tempAward.getUserId());
                        map.put("amount", tempAward.getRecommenderAward().toString());
                        map.put("isFree", "isFree");
                        mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());

                        map = new HashMap<String, String>();
                        map.put("action", "freeze");
                        map.put("productName", "冻结未实名社区注册人奖励");
                        map.put("userId", tempAward.getUserId());
                        map.put("mchOrderNo", "WXSQZC" + tempAward.getUserId());
                        map.put("amount", tempAward.getUserIdAward().toString());
                        map.put("isFree", "isFree");
                        mq4NotifyDemo.send("queue.notify.mch.unfreeze", map.toString());
                        tempAward.setAwardResult(2);
                        zoneUserAwardMapper.updateById(tempAward);
                        System.out.println("冻结未实名社区注册管理员奖励:" + tempAward.getCommunityAdminAward().toString());
                        System.out.println("冻结未实名社区注册推荐人奖励:" + tempAward.getRecommenderAward().toString());
                        System.out.println("冻结未实名社区注册人奖励:" + tempAward.getUserIdAward().toString());
                    }
                }
            }
        }
        return true;
    }

    /***
     * 同上面的方法时一样的 只是返回值增加奖励金额
     * @param regCode
     * @param userId
     * @return
     */
    @Override
    public Map zoneUserAwardResult(String regCode, String userId) {
        Map returnMap = new HashMap();
        returnMap.put("isZoneAward", true);
        String communityAdmin = "";
        String recommender = "";
        ValueOperations operations = stringRedisTemplate.opsForValue();
        Object obj = operations.get("card_pack_zone_remainder");
        BigDecimal remainder = new BigDecimal(150.000);
        BigDecimal line = new BigDecimal(50);
        //获取下级剩余可注册数量
        ResponseObject responseObject = regCodeUsedCount(regCode);
        Map map1 = (Map) responseObject.getData();
        int remain = (int) map1.get("remain");
        ZoneUserAward zoneUserAward = new ZoneUserAward();
        if (obj == null) { // redis 初始化
            operations.set("card_pack_zone_remainder", 150.000 + "");
        } else {
            remainder = new BigDecimal(String.valueOf(obj));
        }
        if (remainder.compareTo(line) <= 0) {
            returnMap.put("isZoneAward", false);
            return returnMap;
        } else {
            BigDecimal times = new BigDecimal(0.001);
            BigDecimal newRemainder = remainder.subtract(times);
            Map<String, String> map = userDataService.checkUserParentDegree(regCode);
            int money = newRemainder.intValue();
            if (null == map) {
                returnMap.put("isZoneAward", false);
                return returnMap;
            } else if (map.get("userType").equals("0")) {
                returnMap.put("isZoneAward", false);
                return returnMap;
            } else if (map.get("userType").equals("1")) {//上级是群主
                communityAdmin = map.get("user1id");
                recommender = map.get("user1id");
            } else if (map.get("userType").equals("2")) {//上级的上级是群主
                communityAdmin = map.get("user2id");
                recommender = map.get("user1id");
            }
            if (!map.get("userType").equals("0") && remain > 0) {
                zoneUserAward.setCommunityAdmin(communityAdmin);
                zoneUserAward.setRecommender(recommender);
                zoneUserAward.setUserId(userId);
                zoneUserAward.setAmount(money);
                zoneUserAward.setAwardArrive(remainder.doubleValue());
                try {
                    operations.set("card_pack_zone_remainder", newRemainder.setScale(3, BigDecimal.ROUND_UP) + "");
                    BigDecimal total = new BigDecimal(150.000);
                    Double d = total.subtract(newRemainder).doubleValue() * 1000;
                    operations.set("card_pack_zone_reg", d.intValue() + "");

                    System.out.println("开始发放社区注册奖励：" + userId);
                    ResponseObject rob = rewardByCount.doWxCommunityReward(communityAdmin, recommender, userId, money + "");
                    System.out.println("发放社区注册奖励结果：" + rob.getErroCode());
                    Object strObj = rob.getData();
                    if (rob.getErroCode().equals(1)) {//领取失败
                        zoneUserAward.setAwardResult(0);
                        zoneUserAward.setUserIdAward(0);
                    }
                    if (rob.getErroCode().equals(0)) {//领取成功
                        zoneUserAward.setAwardResult(1);
                        JSONObject jsonObject = JSON.parseObject(String.valueOf(strObj));
                        Integer communityAdmin_award = Integer.parseInt(jsonObject.get("communityAdmin").toString());
                        Integer recommender_award = Integer.parseInt(jsonObject.get("recommender").toString());
                        Integer userId_award = Integer.parseInt(jsonObject.get("userId").toString());
                        returnMap.put("userId_award", userId_award);
                        zoneUserAward.setCommunityAdminAward(communityAdmin_award);
                        zoneUserAward.setRecommenderAward(recommender_award);
                        zoneUserAward.setUserIdAward(userId_award);
                        zoneUserAward.setOrderid(jsonObject.get("orderId").toString());
                        //奖励之后,回填
                        //上级是群主
                        if (recommender.equals(communityAdmin)) {
                            CommunityUser communityUserInfo = new CommunityUser();
                            communityUserInfo.setUser_id(recommender);
                            communityUserInfo.setHtl_count(recommender_award + communityAdmin_award);//推荐奖励+群主奖励
                            communityUserInfo.setUser_count(1);
                            communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                        } else {
                            //上级的上级是群主
                            CommunityUser communityUserInfo = new CommunityUser();
                            communityUserInfo.setUser_id(communityAdmin);
                            communityUserInfo.setHtl_count(communityAdmin_award);
                            communityUserInfo.setTwo_user_count(1);
                            communityUserMapper.updateCommunityUserInfo(communityUserInfo);
                        }
                    }
                } catch (Exception e) {
                    System.out.println("来自微信发放社区注册奖励异常：" + e.getMessage());
                    zoneUserAward.setAmount(0);
                    zoneUserAward.setAwardResult(0);
                    e.printStackTrace();
                }
                zoneUserAwardMapper.insert(zoneUserAward);
            }
            returnMap.put("isZoneAward", true);
            returnMap.put("awardResult", zoneUserAward.getAwardResult());
            returnMap.put("award", zoneUserAward.getUserIdAward());
            return returnMap;
        }
    }

    @Override
    public ResponseObject regCodeUsedCount(String regCode) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("注册码为空");
        Map map = new HashMap<>();
        int referCodeCount;
        if (StringUtils.isBlank(regCode)) {
            return res;
        }
        Wrapper<User> wrapperuser = new EntityWrapper<User>();
        wrapperuser.eq("reg_code", regCode);
        User parentUser = userDataService.selectOne(wrapperuser);
        if (parentUser == null) {
            res.setData("注册码不存在");
            return res;
        }
//        Map<String, Object> item = sysDicService.selectListByCodeNo("YQZCXZ", "MAX");
       int max = userDataService.getRegCodeUsedLimitCount();
//        if (item != null) {
//            String value = (String) item.get("dicValue");
//            if (StringUtils.isNotBlank(value)) {
//                try {
//                    max = Integer.parseInt(value);
//                    System.out.println("推荐注册人数限制 max：" + max);
//                } catch (NumberFormatException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        String countStr = stringRedisTemplate.opsForValue().get(regCode);
//        if (StringUtils.isBlank(countStr)) {
//            countStr = "0";
//        }
//
//        referCodeCount = Integer.parseInt(countStr);
//
        int remain = userDataService.getRegCodeRemainCount(regCode);
        String iscommunityUser = parentUser.getIscommunityUser();

        if (iscommunityUser != null && iscommunityUser.equals("1")) {//是群主
            CommunityUser communityUserInfo = communityUserMapper.findByUserId(parentUser.getUserId());
            max = communityUserInfo.getInvite_user_max_count();
            remain = max - communityUserInfo.getUser_count();
        }
        res.setErroCode(0);
        map.put("max", max);
        map.put("remain", remain);
        map.put("count", userDataService.getRegCodeUsedCount(regCode));
        map.put("msg", "处理成功");
        res.setData(map);
        return res;
    }

    /**
     * 三级奖励总额200htl
     * 按照1:2:7 发放
     * (取消群主概念)
     *
     * @param regCode
     * @param userId
     * @return
     */
    @Override
    public Map zoneUserAwardThree(String regCode, String userId) {
        Map returnMap = new HashMap();
        boolean isRegAward=true;
        String communityAdmin = "";
        String recommender = "";
        ZoneUserAwardThree zoneUserAward = new ZoneUserAwardThree();
        Map<String, String> map = userDataService.checkUserParentDegreeThree(regCode);
        communityAdmin = map.get("user2id");//上级的上级
        recommender = map.get("user1id");//直接上级
            zoneUserAward.setCommunityAdmin(communityAdmin);
            zoneUserAward.setRecommender(recommender);
            zoneUserAward.setUserId(userId);
            //随机订单号
            String orderId = createRegOrder("YQZC");
            try {
                System.out.println("开始发放社区注册奖励：" + userId);
                ResponseObject rob = rewardByCount.commonReward("YQZC", orderId, communityAdmin, recommender, userId, "0", "manual");
                System.out.println("发放社区注册奖励结果：" + rob.getErroCode());
                Object strObj = rob.getData();
                if (rob.getErroCode().equals(1)) {//领取失败
                    zoneUserAward.setAwardResult(0);
                    zoneUserAward.setUserIdAward(0);
                }
                if (rob.getErroCode().equals(0)) {//领取成功
                    zoneUserAward.setAwardResult(1);
                    ResponseObject rob2=  JSONObject.parseObject(String.valueOf(strObj),ResponseObject.class);
                    Object strObj2 = rob2.getData();
                    JSONObject jsonObject = JSON.parseObject(String.valueOf(strObj2));
                    Integer communityAdmin_award =0;
                    if(jsonObject.get("communityAdmin")!=null){
                        communityAdmin_award=Integer.parseInt(jsonObject.get("communityAdmin").toString());
                    }
                    Integer recommender_award = Integer.parseInt(jsonObject.get("recommender").toString());
                    Integer userId_award = Integer.parseInt(jsonObject.get("userId").toString());
                    returnMap.put("userId_award", userId_award);
                    zoneUserAward.setCommunityAdminAward(communityAdmin_award);
                    zoneUserAward.setRecommenderAward(recommender_award);
                    zoneUserAward.setUserIdAward(userId_award);
                    zoneUserAward.setOrderid(orderId);
                    zoneUserAward.setType(1);
                }
            } catch (Exception e) {
                System.out.println("来自微信发放社区注册奖励异常：" + e.getMessage());
                zoneUserAward.setAmount(0);
                zoneUserAward.setAwardResult(0);
                e.printStackTrace();
            }
            zoneUserAwardThreeMapper.insert(zoneUserAward);
            returnMap.put("isRegAward", isRegAward);
            returnMap.put("awardResult", zoneUserAward.getAwardResult());
            returnMap.put("award", zoneUserAward.getUserIdAward());
            return returnMap;
    }


    @Override
    public String createRegOrder(String prodCode) {
        return prodCode + System.currentTimeMillis();
    }

    @Override
    public Map zoneUserAwardOne(String userId) {
        Map returnMap = new HashMap();
        boolean isRegAward=true;
        //获取下级剩余可注册数量
            ZoneUserAwardThree zoneUserAward = new ZoneUserAwardThree();
            zoneUserAward.setUserId(userId);
            //随机订单号
            String orderId = createRegOrder("ZCHY");
            try {
                System.out.println("开始发放社区注册奖励：" + userId);
                ResponseObject rob = rewardByCount.commonReward("ZCHY", orderId, "", "", userId, "0", "manual");
                System.out.println("发放社区注册奖励结果：" + rob.getErroCode());
                Object strObj = rob.getData();
                if (rob.getErroCode().equals(1)) {//领取失败
                    zoneUserAward.setAwardResult(0);
                    zoneUserAward.setUserIdAward(0);
                }
                if (rob.getErroCode().equals(0)) {//领取成功
                    zoneUserAward.setAwardResult(1);
                    ResponseObject rob2=  JSONObject.parseObject(String.valueOf(strObj),ResponseObject.class);
                    Object strObj2 = rob2.getData();
                    JSONObject jsonObject = JSON.parseObject(String.valueOf(strObj2));
                    Integer userId_award = Integer.parseInt(jsonObject.get("userId").toString());
                    returnMap.put("userId_award", userId_award);
                    zoneUserAward.setUserIdAward(userId_award);
                    zoneUserAward.setOrderid(orderId);
                    zoneUserAward.setType(0);
                }
            } catch (Exception e) {
                System.out.println("发放社区注册奖励异常：" + e.getMessage());
                zoneUserAward.setAmount(0);
                zoneUserAward.setAwardResult(0);
                e.printStackTrace();
            }
            zoneUserAwardThreeMapper.insert(zoneUserAward);
            returnMap.put("isRegAward", isRegAward);
            returnMap.put("awardResult", zoneUserAward.getAwardResult());
            returnMap.put("award", zoneUserAward.getUserIdAward());
            return returnMap;
    }


}
